<template>
  <el-dialog title="采购计划单明细" :visible="selectImportMaterialDialog" width="70%" class="order-importMaterial-select"
    @close="selectImportMaterialClose">
    <div class="app-container order-importMaterial-box">
      <div class="layout-box">
        <el-row :gutter="10" class="mb8" style="display: flex;align-items: center;">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-menu" size="mini" @click="handleCheckleDetail">查询所有
            </el-button>
          </el-col>
          <div class="material-sort" v-if="$route.query.workType==''">
            <span :class="{'sort-active':sortIndex==index}" v-for="(item,index) in sortData" :key="index"
              @click="handleSort(item,index)">{{item.sortName}}</span>
          </div>
        </el-row>

        <!-- 明细列表 -->
        <el-table :data="detailTableData" border style="width: 100%" v-loading="detailLoading" :height="detailMaxHeight"
          @selection-change="handleSelectionChange">
          <el-table-column prop="ppNumber" label="生产订单号" align="center" width="120">
          </el-table-column>
          <el-table-column prop="ppDate" label="需求日期" align="center" width="120">
            <template v-slot="scope">
              <span v-if="scope.row.ppDate!==undefined&&scope.row.ppDate!==null">{{scope.row.ppDate | inMaterTime}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="invCode" label="物料编码" align="center">
          </el-table-column>
          <el-table-column prop="invName" label="物料名称" align="center">
          </el-table-column>
          <el-table-column prop="invAttribute" label="型号规格" align="center">
          </el-table-column>
          <el-table-column prop="unitName" label="主计量" align="center" width="100">
          </el-table-column>
          <el-table-column prop="wmQuantity" label="库存数量" align="center">
            <template v-slot="scope">
              <div style="text-align: right;">
                <span>{{scope.row.wmQuantitye}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="surplus" label="采购计划余量" align="center">
            <template v-slot="scope">
              <div style="text-align: right;">
                <span>{{scope.row.surplus}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="remarks" label="采购说明" align="center">
          </el-table-column>
          <el-table-column type="selection" width="50" align="center">
          </el-table-column>
        </el-table>
      </div>
      <!-- 明细分页 -->
      <pagination v-if="detailTotal>20" :total="detailTotal" :page.sync="queryDetail.pageNum"
        :limit.sync="queryDetail.pageSize" @pagination="getDetailList" />
    </div>
    <div class="ImportMaterial-success">
      <div>
        <el-button :disabled="successStatus" type="primary" @click="handleImportMaterialSuccess">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
  import {
    listDetail, //查询明细
    materialSort
  } from "../../api/order";

  export default {
    data() {
      return {
        // 已选物料
        selectData: [],
        // 明细遮罩层
        detailLoading: true,
        //明细查询参数
        queryDetail: {
          pageNum: 1,
          pageSize: 100,
          sortId: "",
          workType: this.$route.query.workType,
        },

        // 小数位数
        decimal: 2,
        // 明细总条数
        detailTotal: 0,
        // 明细最大高度
        detailMaxHeight: null,
        //物料大类数据
        sortData: [],
        //物料大类下标
        sortIndex: null,
        //明细数据
        detailTableData: [],
        successStatus: true
      }
    },
    props: {
      selectImportMaterialDialog: {
        type: Boolean,
        default: true
      }

    },
    created() {
      this.getMaterialSort();
      this.getDetailList();
    },
    methods: {
      //查询所有明细
      handleCheckleDetail() {
        this.queryDetail = {
          pageNum: 1,
          pageSize: 100,
          sortId: ""
        };
        this.sortIndex = null;
        this.getDetailList()
      },

      // 获取明细
      getDetailList() {
        this.detailLoading = true;
        listDetail(this.queryDetail).then(res => {
          if (res.code == 200) {
            this.detailLoading = false;
            this.detailTableData = res.rows;
            this.detailTotal = res.total;
            this.getDecimal()
            this.detailMaxHeight = (`${document.documentElement.clientHeight}` - 390);
          }

        })
      },

      //获取物料大类
      getMaterialSort() {
        materialSort().then(res => {
          this.sortData = res.data;
        })
      },

      //点击料品大类
      handleSort(data, index) {
        this.sortIndex = index;
        this.queryDetail.sortId = data.sortId;
        this.getDetailList()
      },

      //批量选择
      handleSelectionChange(row) {
        this.selectData = [];
        if (row.length > 0) {
          this.successStatus = false;
          this.selectData.push(...row)
        } else {
          this.successStatus = true;

        }
      },

      //格式化数量单价金额小数位数
      getDecimal() {
        if (this.detailTableData.length > 0) {
          this.detailTableData.forEach(item => {
            if (item.price !== null && item.price !== undefined) {
              item.price = parseFloat(item.price).toFixed(this.decimal);
            }
            if (item.surplus !== null && item.surplus !== undefined) {
              item.surplus = parseFloat(item.surplus).toFixed(this.decimal)
            }
            if (item.amount !== null && item.amount !== undefined) {
              item.amount = parseFloat(item.amount).toFixed(2)
            }
            if (item.wmQuantity !== null && item.wmQuantity !== undefined) {
              item.wmQuantity = parseFloat(item.wmQuantity).toFixed(this.decimal)
            }
            if (item.orderQuantity !== null && item.orderQuantity !== undefined) {
              item.wmQuantity = parseFloat(item.orderQuantity).toFixed(this.decimal)
            }
            if (item.wiQuantity !== null && item.wiQuantity !== undefined) {
              item.wiQuantity = parseFloat(item.wiQuantity).toFixed(this.decimal)
            }
            if (item.woQuantity !== null && item.woQuantity !== undefined) {
              item.woQuantity = parseFloat(item.woQuantity).toFixed(this.decimal)
            }
          })
        }

      },
      // 弹框状态
      selectImportMaterialClose() {
        this.$emit("selectImportMaterialClose");
      },


      //确定选取
      handleImportMaterialSuccess() {
        this.$emit("handleImportMaterialSuccess", this.selectData);
        this.selectImportMaterialClose()
      },

      // 关闭弹框
      handleClose() {
        this.selectImportMaterialClose()
      }
    },
    filters: {
      //时间格式化
      inMaterTime(date) {
        let time = new Date(date);
        return time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate();
      },

      //PP专用
      isPurpose(i) {
        switch (i) {
          case '0':
            return "否"
            break;
          case '1':
            return "是"
            break;
          default:
            return "否"
        }
      }
    }
  };
</script>
<style lang="scss">
  .order-importMaterial-select {
    .el-dialog__body {
      height: calc(100vh - 250px);
      position: relative;
    }

    .el-pagination {
      left: 0;
    }

    .app-container {
      padding-top: 0;
    }

    .layout-box {
      .el-link.is-underline:hover:after {
        border: none;
      }

      .search-box {
        display: flex;
        margin-bottom: 20px;

        .search-input {
          display: flex;
          margin-right: 15px;

          .input-data {
            min-width: 220px !important;
          }

          .el-input {
            margin-right: 15px;
          }
        }
      }

      .sort-active {
        background: #1890ff !important;
        border-color: #1890ff !important;
        color: #FFFFFF !important;
      }

      .con-top {
        display: flex;
        justify-content: space-between;
        align-items: center;

      }

      .material-sort {
        span {
          font-size: 14px;
          padding: 0px 15px;
          background: #f4f4f5;
          color: #999;
          display: inline-block;
          border-radius: 3px;
          margin-left: 5px;
          margin-right: 5px;
          cursor: pointer;
          line-height: 26px;
          border: 1px solid #e9e9eb;
        }
      }
    }

    .ImportMaterial-success {
      position: absolute;
      bottom: 20px;
      right: 40px;

      button {
        padding: 8px 40px;
      }

    }
  }
</style>
